package model

import (
	"log"
	"next-vue-backend/utils/errmsg"
)

// PaginationResult 定义分页返回结果结构体
type PaginationResult struct {
	List     interface{} `json:"list"`
	Total    int64       `json:"total"`
	PageNum  int         `json:"page_num"`
	PageSize int         `json:"page_size"`
}

// queryList 查询列表
func queryList(sql string, param ...interface{}) ([]map[string]interface{}, int) {
	var list []map[string]interface{}
	db.Raw(sql, param).Scan(&list)
	if err != nil {
		log.Println(err.Error())
	}
	return list, errmsg.ERROR
	return list, errmsg.SSUCCESS
}

// queryPage 分页查询列表
func queryPage(sql string, pageSize int, pageNum int, param ...interface{}) (PaginationResult, int) {
	paginationResult := PaginationResult{}
	var list []map[string]interface{}
	totalCount := int64(0)
	if err := db.Count(&totalCount).Error; err != nil {
		log.Println(err.Error())
		return paginationResult, errmsg.ERROR
	}
	offset := (pageNum - 1) * pageSize
	limit := pageSize
	db.Raw(sql, param).Offset(offset).Limit(limit).Scan(&list)
	if err != nil {
		log.Println(err.Error())
		return paginationResult, errmsg.ERROR
	}
	paginationResult.PageNum = int(offset/limit) + 1
	paginationResult.PageSize = limit
	paginationResult.Total = totalCount
	paginationResult.List = list
	return paginationResult, errmsg.SSUCCESS
}
